﻿using SF.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.admin.door
{
    public partial class door_templates_list : Web.UI.ManagePage
    {
        protected int totalCount;
        protected int page;
        protected int pageSize;
        public string keywords = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.pageSize = GetPageSize(10); //每页数量
            this.keywords = DTRequest.GetQueryString("keywords");
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("door_templates_list", DTEnums.ActionEnum.View.ToString()); //检查权限
                RptBind("id>0 " + CombSqlTxt(this.keywords));
            }
        }

        #region 数据绑定=================================
        private void RptBind(string _strWhere)
        {
            this.page = DTRequest.GetQueryInt("page", 1);
            this.txtKeywords.Text = this.keywords;
            BLL.sf_door_templates templateBll = new BLL.sf_door_templates();
            this.rptList.DataSource = templateBll.GetList(this.pageSize, this.page, _strWhere, "id asc", out this.totalCount);
            this.rptList.DataBind();
            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("door_templates_list.aspx", "keywords={0}&page={1}", this.keywords, "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and templatesNum like '%" + _keywords + "%'");
            }

            return strTemp.ToString();
        }
        #endregion


        #region 返回图文每页数量
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("door_templates_page_size"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("door_templates_list.aspx", "keywords={0}", txtKeywords.Text.Trim()));
        }

        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("door_templates_page_size", _pagesize.ToString(), 43200);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("door_templates_list.aspx", "keywords={0}", txtKeywords.Text.Trim()));
        }

        //批量删除
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("door_templates_list", DTEnums.ActionEnum.Delete.ToString()); //检查权限
            int sucCount = 0;
            int errorCount = 0;
            BLL.sf_goods_color colorBll = new BLL.sf_goods_color();
            //批量删除
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                long id = Convert.ToInt64(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    if (colorBll.Delete(id))
                    {
                        sucCount += 1;
                    }
                    else
                    {
                        errorCount += 1;
                    }
                }
            }
            AddAdminLog(DTEnums.ActionEnum.Delete.ToString(), "删除门户模版信息成功" + sucCount + "条，失败" + errorCount + "条"); //记录日志
            JscriptMsg("删除成功" + sucCount + "条，失败" + errorCount + "条！",
                Utils.CombUrlTxt("door_templates_list.aspx", "keywords={0}", txtKeywords.Text.Trim()));
        }

        /// <summary>
        /// 获取 模版 名称
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public string getTemplatesName(string num)
        {
            return new BLL.sf_modeconfig().GetModel("settingNum='" + num + "'").settingname;
        }

        /// <summary>
        /// 传入图片字符串
        /// </summary>
        /// <param name="uris">图片uri</param>
        /// <returns>多张图片</returns>
        public string getAllSearch(string uris)
        {
            string res = "";
            string[] imgs = uris.Split(',');
            for (int i = 0; i < imgs.Length; i++)
            {
                if (imgs[i] != "")
                {
                    res += "<a href=\"###\" style=\"padding-right:5px;\" onclick=\"OrderPrint('" + imgs[i] + "');\">查看</a>";
                }
            }
            res = res != "" ? res : "无";
            return res;
        }
    }
}